<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/3/24
 * Time: 10:31
 */

namespace app\http\middleware;


use think\facade\Cache;

class CheckToken
{
    /**
     * tp5.1 架构 中间件
     * @param $request
     * @param \Closure $next
     * @return mixed|\think\response\Json
     */
    public function handle($request, \Closure $next)
    {
        $token = $request->header('token');
        if ( empty($token) ) {
            return json(['status' => 0, 'msg' => '没有访问权限，请携带合法的token']);
        }
        if ( Cache::get('token') !== $token ) {
            return json(['status' => 0, 'msg' => '没有访问权限，token不合法']);
        }
//        if (! Cache::has('token') ) {
//            return json(['status' => 0, 'msg' => '没有访问权限，token不合法']);
//        }
        return $next($request);  // 请求正常往后走
    }
}